package android.slc.module.sys.ui.dict.data

import android.slc.base.ui.dialog.SlcFastDialogUtils
import android.slc.fast.vd.list.BaseListPageViewVdByFx
import android.slc.base.ui.widget.table.TableViewUtils
import android.slc.appdb.sys.entity.CsDictData
import android.slc.box.commonlibrary.compat.SlcResourceUtils
import javafx.fxml.FXMLLoader
import javafx.scene.Node
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.scene.control.TableCell
import javafx.scene.control.TableColumn

open class DictDataPageVd : BaseListPageViewVdByFx<CsDictData, DictDataPageVmSub>() {

    var editDictDataAction: ((CsDictData) -> Unit)? = null

    protected lateinit var tcDictLabel: TableColumn<CsDictData, String>
    protected lateinit var tcDictValue: TableColumn<CsDictData, String>
    protected lateinit var tcStatus: TableColumn<CsDictData, String>
    protected lateinit var tcSort: TableColumn<CsDictData, Int>
    protected lateinit var tcRemark: TableColumn<CsDictData, String>
    protected lateinit var tcAction: TableColumn<CsDictData, Long>

    override fun initSwipeRecycler() {
        super.initSwipeRecycler()
        initColumns()
    }

    protected open fun initColumns() {
        tcDictLabel = tableView.columns[0] as TableColumn<CsDictData, String>
        tcDictLabel.setCellFactory { getTextItem() }
        tcDictValue = tableView.columns[1] as TableColumn<CsDictData, String>
        tcDictValue.setCellFactory { getTextItem() }
        tcStatus = tableView.columns[2] as TableColumn<CsDictData, String>
        tcStatus.setCellFactory { getTextItem() }
        tcSort = tableView.columns[3] as TableColumn<CsDictData, Int>
        tcSort.setCellFactory { getTextItem() }
        tcRemark = tableView.columns[4] as TableColumn<CsDictData, String>
        tcRemark.setCellFactory { getTextItem() }
        tcAction = tableView.columns[5] as TableColumn<CsDictData, Long>
        tcAction.setCellFactory { getActionItem() }
        TableViewUtils.setHeadLabelCenter(tableView, true)
    }

    protected open fun <T> getTextItem(): TableCell<CsDictData, T?> {
        return object : TableCell<CsDictData, T?>() {
            override fun updateItem(item: T?, empty: Boolean) {
                super.updateItem(item, empty)
                if (!empty) {
                    val itemView = getTextItemFxml()
                    (itemView.lookup("#value") as Label).text = item?.toString()
                    graphic = itemView
                } else {
                    graphic = null
                }
            }
        }
    }

    protected open fun getTextItemFxml(): Node {
        return FXMLLoader.load(DictDataPageVd::class.java.getResource("DictDataItemText.fxml"))
    }

    protected open fun getActionItem(): TableCell<CsDictData, Long?> {
        return object : TableCell<CsDictData, Long?>() {
            override fun updateItem(item: Long?, empty: Boolean) {
                super.updateItem(item, empty)
                if (!empty) {
                    val itemView = getActionItemFxml()
                    (itemView.lookup("#btnEdit") as Button).setOnAction {
                        editDictDataAction?.invoke(tableView.items[index])
                    }
                    (itemView.lookup("#btnDelete") as Button).setOnAction {
                        deleteDialog(tableView.items[index])
                    }
                    graphic = itemView
                } else {
                    graphic = null
                }
            }
        }
    }

    protected open fun getActionItemFxml(): Node {
        return FXMLLoader.load(DictDataPageVd::class.java.getResource("DictDataItemAction.fxml"))
    }


    protected open fun deleteDialog(csDictData: CsDictData) {
        SlcFastDialogUtils.createBuilder().setContentText(SlcResourceUtils.getString("label_delete_really")).build()
            .showAndWait().ifPresent { itButtonType ->
                if (SlcFastDialogUtils.btnIsOk(itButtonType)) {
                    vmSub.onDelete(csDictData)
                }
            }
    }
}